﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class AlterUserInfo : System.Web.UI.Page
{
    private string userName;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userName"] != null)
        {
           userName=Session["userName"].ToString();
           if (!IsPostBack)
           {
                BindDdlist();
                ReadUserDetails();
                if (Session["roleID"].ToString() != "1")
                {
                    ddlDep.Enabled = false;
                }                
                //AddDialogBox();
            }
         
        }
        else
        {
            Response.Redirect(GetUrlPath.GetUrl + "/Login.aspx");
        }

    }
    //提交更新的信息,提交前通过检查Page.IsValid值判断服务器验证是否通过，通过就更改用户信息
    protected void btnTijiao_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            
            Users currentUser = new Users();
            currentUser.UserName = txtName.Text.Trim();
            if (txtNewPwd.Text != "")
            {
                currentUser.UserPwd = txtNewPwd.Text.Trim();
            }
            else
            {
                currentUser.UserPwd = txtOldPwd.Text.Trim(); 
            }
            currentUser.TrueName = txtTrueName.Text.Trim();
            currentUser.DepID = Convert.ToInt32(ddlDep.SelectedValue);
            currentUser.Phone = txtPhone.Text.Trim();
            currentUser.Email = txtEmail.Text.Trim();
            //调用USERS类的更新用户方法
            if (currentUser.UpateUser(userName))
            {
                Response.Write("<script language='javascript'>alert('修改成功！')</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('修改失败！')</script>");
            }

        }
    }
    //服务端验证用户输入的旧密码是否正确
    protected void customValidOldPwd_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string oldPwd = Encrypt.toMD5(args.Value);
        string strSearchPwd = "select PASSWD from USERS where USERNAME='" + userName + "'";
        DBOperation db = new DBOperation();
        if (oldPwd == Convert.ToString(db.ExcuteScalar(strSearchPwd)))
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }
    protected void BindDdlist()
    {
       
        //读取部门名称，绑定到ddlDep下拉列表

        string strSelectDep = "select * from DEPARTMENT";
        DBOperation db = new DBOperation();
        SqlDataReader sdr = db.GetDataReader(strSelectDep);
        ddlDep.DataSource = sdr;
        ddlDep.DataTextField = "DEPNAME";
        ddlDep.DataValueField = "DEPID";
        ddlDep.DataBind();
        //绑定数据后及时关闭连接
        sdr.Close();
    }
    //读取用户原来的数据，绑定到页面控件显示
    protected void ReadUserDetails()
    {
        txtName.Text = userName;
        if (userName == "admin")
        {
            txtName.Enabled = false;
            ddlDep.Enabled = false;
        }
        DataRow dr = Users.searchUser(userName);
        txtTrueName.Text = dr["TRUENAME"].ToString();
        txtPhone.Text = dr["PHONE"].ToString();
        txtEmail.Text = dr["EMAIL"].ToString();
        ddlDep.SelectedValue = dr["DEPID"].ToString();
    }
    //取消方法
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtEmail.Text = "";
        txtName.Text = "";
        txtNewPwd.Text = "";
        txtOldPwd.Text = "";
        txtPhone.Text = "";
        txtTrueName.Text = "";
        reTexNewPwd.Text = "";
    }
    //为更改提交添加确认对话框
    protected void AddDialogBox()
    {
        btnTijiao.Attributes.Add("onClick", "return confirm('您是否真的要修改这些用户信息？');");
    }


}
